import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    meta:{
      title: '舒恺工作室',
    },
    component: Login
  },
  {
    path: '/',
    name: 'main',
    component: () => import('../views/layout/main.vue'),
    children:[
      {
        path: '/',
        name: 'home',
        meta: {
          title: '首页',
          icon: 'el-icon-monitor'
      },
        component: () => import('../views/home/home.vue'),
      },
      {
        path: 'baseInfo',
        name: 'baseInfo',
        meta: {
          title: '基础信息管理',
          icon: 'el-icon-tickets',
      },
      component: () => import('../components/baseInfo.vue'),
      children:[
        {
          path: 'userManagment',
          name: 'userManagment',
          component: () => import('../views/userMangent/usermangent.vue'),
                      meta: {
                          title: '人员管理',
                          icon: 'el-icon-user',
                      }
        },
        {
          path: 'firmrManagment',
          name: 'firmManagment',
          component: () => import('../views/firm/firmMangent.vue'),
                      meta: {
                          title: '合作公司管理',
                          icon: 'el-icon-office-building',
                      }
        },
        {
          path: 'ceremonyHallMangent',
          name: 'ceremonyHallMangent',
          component: () => import('../views/ceremonyHall/hallMangent.vue'),
                      meta: {
                          title: '合作仪式厅管理',
                          icon: 'el-icon-house',
                      }
        },
        {
          path: 'joinHands',
          name: 'joinHands',
          component: () => import('../views/teamPerson/joinHands.vue'),
                      meta: {
                          title: '合作人员管理',
                          icon: 'el-icon-data-line',
                      }
        }
      ],
      },
      {
        path: '/canlender',
        name: 'canlender',
        meta: {
          title: '档期管理',
          icon: 'el-icon-date'
      },
        component: () => import('../views/canlenderMangent/canlenderIndex.vue'),
      },
      {
        path: '/activity',
        name: 'activity',
        meta: {
          title: '活动管理',
          icon: 'el-icon-pie-chart'
      },
        component: () => import('../views/activeMangent/activeIndex.vue'),
      },
      {
        path: '/funds',
        name: 'funds',
        meta: {
          title: '经费管理',
          icon: 'el-icon-wallet'
      },
        component: () => import('../views/fundsManagement/fundsIndex.vue'),
      },
    ]
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})
 // 路由守卫
 router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  const isLoggedIn = sessionStorage.getItem('isLoggedIn') === 'true';
  
  // 如果用户访问的是登录页面且已经登录，则重定向到首页
  if (to.path === '/login' && isLoggedIn) {
      next('/');
      return;
  }
  
  // 如果用户访问的是需要登录的页面且未登录，则重定向到登录页
  if (to.path !== '/login' && !isLoggedIn) {
      next('/login');
      return;
  }
  
  // 其他情况允许访问
  next();
});


export default router
